using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BezPanike.Domain;
using BezPanike.Domain.Services;

public partial class Default_aspx : System.Web.UI.Page
{
    KorisniciService korisniciService = new KorisniciService();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            phPrijava.Visible = false;
            phStatusPrijave.Visible = true;
            var korisnik = korisniciService.UzmiKorisnikaPoKorisnickomImenu(User.Identity.Name);
            lblStatusPrijave.Text = "Dobrodosli " + korisnik.Ime + " " + korisnik.Prezime;
        }
        else
        {
            phPrijava.Visible = true;
            phStatusPrijave.Visible = false;
        }

        var service = new LinkService();
        var topFive = service.UzmiLinkovePoBrojuPoseta(5);
        TopRepeater.DataSource = topFive;
        TopRepeater.DataBind();

    }

    protected void Prijava_OnClick(object sender, ImageClickEventArgs e)
    {
        Korisnici korisnik = korisniciService.PrijaviKorisnikaPoKorisnickomImenuLozinci(tbKorisnickoIme.Text, tbLozinka.Text);

        if (korisnik != null)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, tbKorisnickoIme.Text, DateTime.Now,
                DateTime.Now.AddMinutes(120), chkZapamtiMe.Checked, "custom data");
            
            string cookieStr = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieStr);

            if (chkZapamtiMe.Checked)
                cookie.Expires = ticket.Expiration;

            cookie.Path = FormsAuthentication.FormsCookiePath;
            Response.Cookies.Add(cookie);

            Response.Redirect("User/Default.aspx", true);
        }
        else
        {
            phStatusPrijave.Visible = true;
            lblStatusPrijave.Text = "Pogresni korisnicko ime ili lozinka!";
        }
    }

    protected void BtnTrazi_Click(object sender, EventArgs e)
    {
        Response.Redirect("Search.aspx?search=" + tbSearch.Text, true);
    }
}